// Name: David Andersen

using UnityEngine;
using System.Collections;

public class ScoreTableGUI : MonoBehaviour {

	public GUISkin guiSkin;
	public GameObject controller;
	public int score;
	
	void Start () {
		controller = GameObject.Find("Controller");
		score = controller.GetComponent<Controller_Script>().score;
		
		// Based on code by Derek Traver from Unity Answers
		if(score > PlayerPrefs.GetInt("Score1"))
		{
			PlayerPrefs.SetInt("Score10", PlayerPrefs.GetInt("Score9"));
			PlayerPrefs.SetInt("Score9", PlayerPrefs.GetInt("Score8"));
			PlayerPrefs.SetInt("Score8", PlayerPrefs.GetInt("Score7"));
			PlayerPrefs.SetInt("Score7", PlayerPrefs.GetInt("Score6"));
			PlayerPrefs.SetInt("Score6", PlayerPrefs.GetInt("Score5"));
			PlayerPrefs.SetInt("Score5", PlayerPrefs.GetInt("Score4"));
			PlayerPrefs.SetInt("Score4", PlayerPrefs.GetInt("Score3"));
			PlayerPrefs.SetInt("Score3", PlayerPrefs.GetInt("Score2"));
			PlayerPrefs.SetInt("Score2", PlayerPrefs.GetInt("Score1"));
			PlayerPrefs.SetInt("Score1", score);
		}
		else if(score > PlayerPrefs.GetInt("Score2"))
		{
			PlayerPrefs.SetInt("Score10", PlayerPrefs.GetInt("Score9"));
			PlayerPrefs.SetInt("Score9", PlayerPrefs.GetInt("Score8"));
			PlayerPrefs.SetInt("Score8", PlayerPrefs.GetInt("Score7"));
			PlayerPrefs.SetInt("Score7", PlayerPrefs.GetInt("Score6"));
			PlayerPrefs.SetInt("Score6", PlayerPrefs.GetInt("Score5"));
			PlayerPrefs.SetInt("Score5", PlayerPrefs.GetInt("Score4"));
			PlayerPrefs.SetInt("Score4", PlayerPrefs.GetInt("Score3"));
			PlayerPrefs.SetInt("Score3", PlayerPrefs.GetInt("Score2"));
			PlayerPrefs.SetInt("Score2", score);
		}
		else if(score > PlayerPrefs.GetInt("Score3"))
		{
			PlayerPrefs.SetInt("Score10", PlayerPrefs.GetInt("Score9"));
			PlayerPrefs.SetInt("Score9", PlayerPrefs.GetInt("Score8"));
			PlayerPrefs.SetInt("Score8", PlayerPrefs.GetInt("Score7"));
			PlayerPrefs.SetInt("Score7", PlayerPrefs.GetInt("Score6"));
			PlayerPrefs.SetInt("Score6", PlayerPrefs.GetInt("Score5"));
			PlayerPrefs.SetInt("Score5", PlayerPrefs.GetInt("Score4"));
			PlayerPrefs.SetInt("Score4", PlayerPrefs.GetInt("Score3"));
			PlayerPrefs.SetInt("Score3", score);
		}
		else if(score > PlayerPrefs.GetInt("Score4"))
		{
			PlayerPrefs.SetInt("Score10", PlayerPrefs.GetInt("Score9"));
			PlayerPrefs.SetInt("Score9", PlayerPrefs.GetInt("Score8"));
			PlayerPrefs.SetInt("Score8", PlayerPrefs.GetInt("Score7"));
			PlayerPrefs.SetInt("Score7", PlayerPrefs.GetInt("Score6"));
			PlayerPrefs.SetInt("Score6", PlayerPrefs.GetInt("Score5"));
			PlayerPrefs.SetInt("Score5", PlayerPrefs.GetInt("Score4"));
			PlayerPrefs.SetInt("Score4", score);
		}
		else if(score > PlayerPrefs.GetInt("Score5"))
		{
			PlayerPrefs.SetInt("Score10", PlayerPrefs.GetInt("Score9"));
			PlayerPrefs.SetInt("Score9", PlayerPrefs.GetInt("Score8"));
			PlayerPrefs.SetInt("Score8", PlayerPrefs.GetInt("Score7"));
			PlayerPrefs.SetInt("Score7", PlayerPrefs.GetInt("Score6"));
			PlayerPrefs.SetInt("Score6", PlayerPrefs.GetInt("Score5"));
			PlayerPrefs.SetInt("Score5", score);
		}
		else if(score > PlayerPrefs.GetInt("Score6"))
		{
			PlayerPrefs.SetInt("Score10", PlayerPrefs.GetInt("Score9"));
			PlayerPrefs.SetInt("Score9", PlayerPrefs.GetInt("Score8"));
			PlayerPrefs.SetInt("Score8", PlayerPrefs.GetInt("Score7"));
			PlayerPrefs.SetInt("Score7", PlayerPrefs.GetInt("Score6"));
			PlayerPrefs.SetInt("Score6", score);
		}
		else if(score > PlayerPrefs.GetInt("Score7"))
		{
			PlayerPrefs.SetInt("Score10", PlayerPrefs.GetInt("Score9"));
			PlayerPrefs.SetInt("Score9", PlayerPrefs.GetInt("Score8"));
			PlayerPrefs.SetInt("Score8", PlayerPrefs.GetInt("Score7"));
			PlayerPrefs.SetInt("Score7", score);
		}
		else if(score > PlayerPrefs.GetInt("Score8"))
		{
			PlayerPrefs.SetInt("Score10", PlayerPrefs.GetInt("Score9"));
			PlayerPrefs.SetInt("Score9", PlayerPrefs.GetInt("Score8"));
			PlayerPrefs.SetInt("Score8", score);
		}
		else if(score > PlayerPrefs.GetInt("Score9"))
		{
			PlayerPrefs.SetInt("Score10", PlayerPrefs.GetInt("Score9"));
			PlayerPrefs.SetInt("Score9", score);
		}
		else if(score > PlayerPrefs.GetInt("Score10"))
		{
			PlayerPrefs.SetInt("Score10", score);
		}
		
		Destroy(controller);
		
	}
	
	void OnGUI () {
		GUI.skin = guiSkin;
		
		GUILayout.BeginArea(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 300, 600, 600));
		GUILayout.BeginVertical();
		
		GUILayout.Box("High Scores");
		GUILayout.Box(PlayerPrefs.GetInt("Score1").ToString());
		GUILayout.Box(PlayerPrefs.GetInt("Score2").ToString());
		GUILayout.Box(PlayerPrefs.GetInt("Score3").ToString());
		GUILayout.Box(PlayerPrefs.GetInt("Score4").ToString());
		GUILayout.Box(PlayerPrefs.GetInt("Score5").ToString());
		GUILayout.Box(PlayerPrefs.GetInt("Score6").ToString());
		GUILayout.Box(PlayerPrefs.GetInt("Score7").ToString());
		GUILayout.Box(PlayerPrefs.GetInt("Score8").ToString());
		GUILayout.Box(PlayerPrefs.GetInt("Score9").ToString());
		GUILayout.Box(PlayerPrefs.GetInt("Score10").ToString());
		if(GUILayout.Button("Main Menu"))
		{
			Application.LoadLevel("TitleScreen");
		}
		
		GUILayout.EndVertical();
		GUILayout.EndArea();
	}
}
